import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/Login.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/Dashboard.vue'),
        meta: { title: '首页', icon: 'House' }
      }
    ]
  },
  {
    path: '/household',
    component: Layout,
    redirect: '/household/register',
    meta: { title: '户籍管理', icon: 'UserFilled' },
    children: [
      {
        path: 'register',
        name: 'HouseholdRegister',
        component: () => import('@/views/household/register/HouseholdRegister.vue'),
        meta: { title: '家庭户口立户', icon: 'Plus' }
      },
      {
        path: 'query',
        name: 'HouseholdQuery',
        component: () => import('@/views/household/query/HouseholdQuery.vue'),
        meta: { title: '综合查询', icon: 'Search' }
      },
      {
        path: 'householder-query',
        name: 'HouseholderQuery',
        component: () => import('@/views/household/householder-query/HouseholderQuery.vue'),
        meta: { title: '户主查询', icon: 'User' }
      }
    ]
  },
  {
    path: '/resident-population',
    component: Layout,
    redirect: '/resident-population/move-in/birth',
    meta: { title: '常驻人口管理', icon: 'Coordinate' },
    children: [
      {
        path: 'move-in',
        meta: { title: '户口迁入', icon: 'Right' },
        children: [
          {
            path: 'birth',
            name: 'BirthReport',
            component: () => import('@/views/household/birth/BirthRegistration.vue'),
            meta: { title: '出生申报', icon: 'Avatar' }
          },
          {
            path: 'spouse-join',
            name: 'SpouseJoin',
            component: () => import('@/views/household/spouse-join/SpouseJoin.vue'),
            meta: { title: '夫妻投靠', icon: 'Connection' }
          },
          {
            path: 'child-join',
            name: 'ChildJoin',
            component: () => import('@/views/household/child-join/ChildJoin.vue'),
            meta: { title: '子女投靠父母', icon: 'Promotion' }
          }
        ]
      },
      {
        path: 'move-out',
        meta: { title: '户口迁出', icon: 'Back' },
        children: [
          {
            path: 'death',
            name: 'DeathCancel',
            component: () => import('@/views/household/death/DeathRegistration.vue'),
            meta: { title: '死亡注销', icon: 'Remove' }
          },
          {
            path: 'transfer',
            name: 'Transfer',
            component: () => import('@/views/household/transfer/HouseholdTransfer.vue'),
            meta: { title: '户口迁移', icon: 'Sort' }
          }
        ]
      },
      {
        path: 'approval',
        name: 'HouseholdApproval',
        component: () => import('@/views/household/approval/HouseholdApproval.vue'),
        meta: { title: '户籍审批管理', icon: 'Document' }
      }
    ]
  },
  {
    path: '/population',
    component: Layout,
    redirect: '/population/floating',
    meta: { title: '人口管理', icon: 'Coordinate' },
    children: [
      {
        path: 'floating',
        name: 'FloatingPopulation',
        component: () => import('@/views/population/floating/index.vue'),
        meta: { title: '流动人口管理', icon: 'Position' }
      },
      {
        path: 'key-personnel',
        name: 'KeyPersonnel',
        component: () => import('@/views/population/key-personnel/index.vue'),
        meta: { title: '重点人口管理', icon: 'Warning' }
      }
    ]
  },
  {
    path: '/certificate',
    component: Layout,
    redirect: '/certificate/id-card',
    meta: { title: '证件管理', icon: 'Postcard' },
    children: [
      {
        path: 'id-card',
        meta: { title: '身份证件办理', icon: 'CreditCard' },
        children: [
          {
            path: 'query',
            name: 'IdCardQuery',
            component: () => import('@/views/certificate/id-card/index.vue'),
            meta: { title: '查询', icon: 'Search', activeTab: 'query' }
          },
          {
            path: 'apply',
            name: 'IdCardApply',
            component: () => import('@/views/certificate/id-card/index.vue'),
            meta: { title: '办理', icon: 'Plus', activeTab: 'apply' }
          },
          {
            path: 'reissue',
            name: 'IdCardReissue',
            component: () => import('@/views/certificate/id-card/index.vue'),
            meta: { title: '补办', icon: 'Refresh', activeTab: 'reissue' }
          },
          {
            path: 'cancel',
            name: 'IdCardCancel',
            component: () => import('@/views/certificate/id-card/index.vue'),
            meta: { title: '注销', icon: 'Delete', activeTab: 'cancel' }
          }
        ]
      },
      {
        path: 'residence',
        meta: { title: '居住证件办理', icon: 'Tickets' },
        children: [
          {
            path: 'query',
            name: 'ResidenceQuery',
            component: () => import('@/views/certificate/residence/index.vue'),
            meta: { title: '查询', icon: 'Search', activeTab: 'query' }
          },
          {
            path: 'apply',
            name: 'ResidenceApply',
            component: () => import('@/views/certificate/residence/index.vue'),
            meta: { title: '办理', icon: 'Plus', activeTab: 'apply' }
          },
          {
            path: 'reissue',
            name: 'ResidenceReissue',
            component: () => import('@/views/certificate/residence/index.vue'),
            meta: { title: '补办', icon: 'Refresh', activeTab: 'reissue' }
          },
          {
            path: 'cancel',
            name: 'ResidenceCancel',
            component: () => import('@/views/certificate/residence/index.vue'),
            meta: { title: '注销', icon: 'Delete', activeTab: 'cancel' }
          }
        ]
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 这里可以添加登录验证逻辑
  if (to.meta.requiresAuth !== false) {
    // 检查是否已登录
    const token = localStorage.getItem('token')
    if (!token && to.path !== '/login') {
      next('/login')
      return
    }
  }
  next()
})

export default router